﻿Imports ICSharpCode.AvalonEdit.CodeCompletion
Imports Microsoft.SqlServer.Management.SqlParser.Intellisense

Public Class CompletionItem
    Implements ICompletionData

    Private d As Declaration

    Public Sub New(ByVal d As Declaration)
        Me.d = d

    End Sub
    Public Sub Complete(textArea As ICSharpCode.AvalonEdit.Editing.TextArea, completionSegment As ICSharpCode.AvalonEdit.Document.ISegment, insertionRequestEventArgs As EventArgs) Implements ICompletionData.Complete
        Dim snippet As String = textArea.Document.Text.Substring(0, completionSegment.EndOffset)
        Dim startPos As Integer?
        Dim segmentLength As Integer?
        Dim c As Char
        For i As Integer = snippet.Length - 1 To 0 Step -1
            c = snippet.Substring(i, 1)
            If c = " " OrElse c = "[" OrElse c = "(" OrElse c = "." OrElse c = "@" Then
                startPos = i + 1
                segmentLength = completionSegment.EndOffset - startPos
                Exit For
            End If
        Next
        Dim replacementText As String = d.Title
        If Not IsNothing(startPos) AndAlso segmentLength < replacementText.Length Then
            replacementText = replacementText.Substring(segmentLength, d.Title.Length - segmentLength)
        End If
        textArea.Document.Replace(completionSegment, replacementText)
    End Sub

    Public ReadOnly Property Content As Object Implements ICompletionData.Content
        Get
            Return d.Title
        End Get
    End Property

    Public ReadOnly Property Description As Object Implements ICompletionData.Description
        Get
            Return d.Description
        End Get
    End Property

    Public ReadOnly Property Image As ImageSource Implements ICompletionData.Image
        Get
            Dim imgName As String = String.Empty

            Select Case d.Type
                Case DeclarationType.Database
                    imgName = "database.png"
                Case DeclarationType.Table
                    imgName = "table.png"
                Case DeclarationType.Column, DeclarationType.ForeignKeyColumn, DeclarationType.IndexedColumn
                    imgName = "column.png"
                Case DeclarationType.BuiltInFunction, DeclarationType.StoredProcedure
                    If Not d.Description.StartsWith("global variable") Then
                        imgName = "function.png"
                    Else
                        imgName = "variable.png"
                    End If

                Case DeclarationType.ScalarVariable
                    imgName = "variable.png"
                Case Else
                    imgName = String.Empty

            End Select
            If Not String.IsNullOrEmpty(imgName) Then
                Return New BitmapImage(New Uri("pack://application:,,,/SqlAppifier;component/assets/" + imgName, UriKind.Absolute))
            Else
                Return Nothing
            End If

        End Get
    End Property

    Public ReadOnly Property Priority As Double Implements ICompletionData.Priority
        Get
            Select Case d.Type
                Case DeclarationType.Database
                    Return 1
                Case DeclarationType.Table
                    Return 2
                Case DeclarationType.Column, DeclarationType.ForeignKeyColumn, DeclarationType.IndexedColumn
                    Return 3
                Case Else
                    Return 0

            End Select
        End Get
    End Property

    Public ReadOnly Property Text As String Implements ICompletionData.Text
        Get
            Return d.Title
        End Get
    End Property
End Class
